package todo_plan

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"tao-admin/utils"
)

type PlanView struct {
	PlanService         *PlanService
	PlanCalendarService *PlanCalendarService
}

func NewPlanView() *PlanView {
	return &PlanView{
		PlanService:         NewPlanService(),
		PlanCalendarService: NewPlanCalendarService(),
	}
}

func (view *PlanView) searchPlanInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	planInfos, total, err := view.PlanService.GetPlanInfos(ctx)
	if err != nil {
		resp := fmt.Sprintf("error:%v", err)
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": resp,
		}
	} else {
		res.Data = map[string]interface{}{
			"items": planInfos,
			"total": total,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) createPlanInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanService.CreatePlanItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) updatePlanInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanService.UpdatePlanItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) detailPlanInfoView(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"retcode": 0,
		"msg":     "success",
		"data":    "planInfos",
	})
}

func (view *PlanView) deletePlanInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanService.DeletePlanItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) cronPlanInfoView() {
	fmt.Println("Plan定时任务：查找未完成的计划项！！！")
}

func (view *PlanView) getCalendarInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	planInfos, total, err := view.PlanCalendarService.GetPlanCalendarInfos(ctx)
	if err != nil {
		resp := fmt.Sprintf("error:%v", err)
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": resp,
		}
	} else {
		res.Data = map[string]interface{}{
			"schedules": planInfos,
			"total":     total,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) createPlanCalendarInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanCalendarService.CreatePlanCalendarItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) updatePlanCalendarInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanCalendarService.UpdatePlanCalendarItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}

func (view *PlanView) deletePlanCalendarInfoView(ctx *gin.Context) {
	res := utils.NewSimpleRes()
	err, msg := view.PlanCalendarService.DeletePlanCalendarItem(ctx)
	if err != nil || msg != "" {
		res.Retcode = -1
		res.Message = "failed"
		res.Data = map[string]interface{}{
			"err": err,
			"msg": msg,
		}
	}
	ctx.JSON(200, res)
}
